﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Collections.Generic;
using System.IO;

namespace LTP.Common
{
    public class TreeClass
    {
        public TreeClass()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 初始化树
        /// </summary>
        public static void InitTreeViewNodes(TreeView TV, TreeNode ParentNode, Guid parentID, DataTable menuTree, string strID, string strPID, string strName, string strOrder, string parentName, string jsFun, bool isCheck)
        {
            if (menuTree == null)
            {
                return;
            }
            if (parentID != new Guid())
            {
                parentName = parentName + "/" + menuTree.Select(strID + " = '" + parentID.ToString() + "'", strID)[0][strName].ToString();
            }

            DataRow[] rowList;
            if (parentID != new Guid())
            {
                rowList = menuTree.Select(strPID + " = '" + parentID.ToString() + "'", strOrder + " asc," + strID + " asc");
            }
            else
            {
                rowList = menuTree.Select(strPID + " = '" + parentID.ToString() + "'", strOrder + " asc," + strID + " desc");
            }
            foreach (DataRow row in rowList)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = row[strName].ToString();
                newNode.Value = row[strID].ToString();
                newNode.CollapseAll();
                if (ParentNode == null)
                {
                    TV.Nodes.Add(newNode);
                }
                else
                {
                    ParentNode.ChildNodes.Add(newNode);
                }
                if (ParentNode != null && !isCheck && jsFun != "")
                {
                    newNode.NavigateUrl = "javascript:" + jsFun + "('" + row[strID].ToString() + "', '" + row[strName].ToString() + "');";
                }
                else
                {
                    if (isCheck)
                    {
                        newNode.ShowCheckBox = true;
                    }
                    else
                    {
                        newNode.ShowCheckBox = false;
                    }
                    newNode.SelectAction = TreeNodeSelectAction.None;
                }
                InitTreeViewNodes(TV, newNode, new Guid(row[strID].ToString()), menuTree, strID, strPID, strName, strOrder, parentName, jsFun, isCheck);
            }
        }

        /// <summary>
        /// 初始化树
        /// </summary>
        public static void InitTreeViewNodes(TreeView TV, TreeNode ParentNode, Guid parentID, DataTable menuTree, string strID, string strPID, string strName,string strCount, string strOrder, string parentName, string jsFun, bool isCheck)
        {
            if (menuTree == null)
            {
                return;
            }
            if (parentID != new Guid())
            {
                parentName = parentName + "/" + menuTree.Select(strID + " = '" + parentID.ToString() + "'", strID)[0][strName].ToString();
            }

            DataRow[] rowList;
            if (parentID != new Guid())
            {
                rowList = menuTree.Select(strPID + " = '" + parentID.ToString() + "'", strOrder + " asc," + strID + " asc");
            }
            else
            {
                rowList = menuTree.Select(strPID + " = '" + parentID.ToString() + "'", strOrder + " asc," + strID + " desc");
            }
            foreach (DataRow row in rowList)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = row[strName].ToString();
                if (row[strCount].ToString() != "0")
                {
                    newNode.Text += "(" + row[strCount].ToString() + ")";
                }
                newNode.Value = row[strID].ToString();
                newNode.CollapseAll();
                if (ParentNode == null)
                {
                    TV.Nodes.Add(newNode);
                }
                else
                {
                    ParentNode.ChildNodes.Add(newNode);
                }
                if (ParentNode != null && !isCheck && jsFun != "")
                {
                    newNode.NavigateUrl = "javascript:" + jsFun + "('" + row[strID].ToString() + "', '" + row[strName].ToString() + "');";
                }
                else
                {
                    if (isCheck)
                    {
                        newNode.ShowCheckBox = true;
                    }
                    else
                    {
                        newNode.ShowCheckBox = false;
                    }
                    newNode.SelectAction = TreeNodeSelectAction.None;
                }
                InitTreeViewNodes(TV, newNode, new Guid(row[strID].ToString()), menuTree, strID, strPID, strName,strCount, strOrder, parentName, jsFun, isCheck);
            }
        }

        /// <summary>
        /// 初始化树
        /// </summary>
        public static void InitTreeViewNodes2(TreeView TV, TreeNode ParentNode, Guid parentID, DataTable menuTree, string strID, string strPID, string strName, string strCount, string strOrder, string parentName, string jsFun, bool isCheck)
        {
            if (menuTree == null)
            {
                return;
            }
            if (parentID != new Guid())
            {
                parentName = parentName + "/" + menuTree.Select(strID + " = '" + parentID.ToString() + "'", strID)[0][strName].ToString();
            }

            DataRow[] rowList;
            if (parentID != new Guid())
            {
                rowList = menuTree.Select(strPID + " = '" + parentID.ToString() + "'", strOrder + " asc," + strID + " asc");
            }
            else
            {
                rowList = menuTree.Select(strPID + " = '" + parentID.ToString() + "'", strOrder + " asc," + strID + " desc");
            }
            foreach (DataRow row in rowList)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = row[strName].ToString();
                if (row[strCount].ToString() != "0")
                {
                    newNode.Text += "(已报)";
                }
                newNode.Value = row[strID].ToString();
                newNode.CollapseAll();
                if (ParentNode == null)
                {
                    TV.Nodes.Add(newNode);
                }
                else
                {
                    ParentNode.ChildNodes.Add(newNode);
                }
                if (ParentNode != null && !isCheck && jsFun != "")
                {
                    newNode.NavigateUrl = "javascript:" + jsFun + "('" + row[strID].ToString() + "', '" + row[strName].ToString() + "');";
                }
                else
                {
                    if (isCheck)
                    {
                        newNode.ShowCheckBox = true;
                    }
                    else
                    {
                        newNode.ShowCheckBox = false;
                    }
                    newNode.SelectAction = TreeNodeSelectAction.None;
                }
                InitTreeViewNodes2(TV, newNode, new Guid(row[strID].ToString()), menuTree, strID, strPID, strName, strCount, strOrder, parentName, jsFun, isCheck);
            }
        }

        /// <summary>
        /// 初始化树
        /// </summary>
        public static void InitTreeViewNodes3(TreeView TV, TreeNode ParentNode, Guid parentID, DataTable menuTree, string strID, string strPID, string strName, string strOrder, string parentName, string jsFun, bool isCheck)
        {
            if (menuTree == null)
            {
                return;
            }
            if (parentID != new Guid())
            {
                parentName = parentName + "/" + menuTree.Select(strID + " = '" + parentID.ToString() + "'", strID)[0][strName].ToString();
            }

            DataRow[] rowList;
            if (parentID != new Guid())
            {
                rowList = menuTree.Select(strPID + " = '" + parentID.ToString() + "'", strOrder + " asc," + strID + " asc");
            }
            else
            {
                rowList = menuTree.Select(strPID + " = '" + parentID.ToString() + "'", strOrder + " asc," + strID + " desc");
            }
            foreach (DataRow row in rowList)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = row[strName].ToString();
                newNode.Value = row[strID].ToString();
                newNode.CollapseAll();
                if (ParentNode == null)
                {
                    TV.Nodes.Add(newNode);
                }
                else
                {
                    ParentNode.ChildNodes.Add(newNode);
                }
                if (ParentNode != null && !isCheck && jsFun != "")
                {
                    newNode.NavigateUrl = "javascript:" + jsFun + "('" + row[strID].ToString() + "', '" + row[strName].ToString() + "');";
                }
                else
                {
                    if (isCheck)
                    {
                        newNode.ShowCheckBox = true;
                    }
                    else
                    {
                        newNode.ShowCheckBox = false;
                    }
                    newNode.SelectAction = TreeNodeSelectAction.None;
                }
                InitTreeViewNodes3(TV, newNode, new Guid(row[strID].ToString()), menuTree, strID, strPID, strName, strOrder, parentName, jsFun, isCheck);
            }
        }

        /// <summary>
        /// 展开
        /// </summary>
        /// <param name="treeNodes"></param>
        /// <param name="treeID"></param>
        public static void ExpandTreeNodes(TreeNodeCollection treeNodes, string treeID)
        {
            foreach (TreeNode node in treeNodes)
            {
                if (node.Value == treeID)
                {
                    OpenTreeNodes(node);
                    break;
                }
                //else
                //{
                //    node.Collapse();
                //    ExpandTreeNodes(node.ChildNodes, treeID);
                //}
                if (node.ChildNodes.Count > 0)
                {
                    ExpandTreeNodes(node.ChildNodes, treeID);
                }
            }
        }

        private static void OpenTreeNodes(TreeNode treeNode)
        {
            treeNode.Expand();
            if (treeNode.Parent != null)
            {
                OpenTreeNodes(treeNode.Parent);
            }
        }
    }
}